package com.cow.sram.module.managerResource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.utils.reflection.BeanToMapUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.dao.ResourceinfoDAO;
import com.cow.sram.data.entity.Resourceinfo;
import com.cow.sram.data.mapper.ResourceinfoMapper;
import com.cow.sram.module.archives.OrgInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 普通资源管理
 * @author Administrator
 *
 */
@Service
public class ResourceinfoService  extends BaseComponent{

	@Autowired
	ResourceinfoDAO resourceInfoDao;
	@Autowired
	ResourceinfoMapper resourceinfoMapper;
	@Autowired
	OrgInfoService orgInfoService;

	
	/**查询全部资源*/
	public Page<Map> findResourceInfo(Map parmMap){
		if (parmMap.get("page") != null && parmMap.get("size") != null) {
			PageHelper.startPage((Integer) parmMap.get("page"), (Integer) parmMap.get("size"));
		}
		com.github.pagehelper.Page<Resourceinfo> list = resourceinfoMapper.queryResourceinfo(parmMap);
		com.github.pagehelper.Page<Map> resultList = new com.github.pagehelper.Page<Map>();
		BeanUtils.copyProperties(list, resultList);
		for (Resourceinfo resInfo : list) {
			resultList.add(convertToMap(resInfo));
		}
		return resultList;

	}
	
	//转译
	private Map convertToMap(Resourceinfo resinfo) {
		Map dictMap = new HashMap();
		dictMap.put("systemid", "ALLSYSTEM");
		dictMap.put("resourcetype", "RESOURCETYPE");
		Map map = BeanToMapUtil.convertBean(resinfo);
		dictUtil.valueToName(map, dictMap);
		return map;
	}
	
	
	/**根据id查询资源*/
	public Resourceinfo getResourceinfoID (Integer id){
		return resourceInfoDao.findOne(id);
	}
	
	/** 根据ID查询记录 */
	public Map getResourceInfoById(Integer id) {
		//转译
		return convertToMap(resourceInfoDao.findOne(id));
		
	}
	
	/**新增资源*/
	public Resourceinfo saveResourceInfo(Resourceinfo rsc){
		return resourceInfoDao.save(rsc);
	}
	
	/**删除资源*/
	public void delResourceInfo(Integer id){
		Resourceinfo resourceinfo=this.getResourceinfoID(id);
		 resourceInfoDao.delete(resourceinfo);
	}
	
	/**
	 * 
	 * @param list
	 * @return
	 * Administrator
	 */
	public List<Map> mapListToTree(List<Map> list){
		List<Map> resultList = new ArrayList<Map>();
		if (list != null) {
			for (Map resourceinfo : list) {
				Integer fatherId = (Integer)resourceinfo.get("fatherId");
				if (fatherId == null || fatherId.equals(-1)) {//最上级节点
					resultList.add(resourceinfo);
				}
				for (Map resource : list) {
					
					if (resource.get("fatherId").equals(resourceinfo.get("resourceid"))) {
						if (resourceinfo.get("childList") == null ) {
							List<Map> myChildrens = new ArrayList<Map>();
							myChildrens.add(resource);
							resourceinfo.put("childList",myChildrens);
						}else{
							List<Map> myChildrens = (List<Map>)resourceinfo.get("childList");
							myChildrens.add(resource);
						}
					}
				}
			}
		}
		return resultList;
	}
	
	/**
	 * 
	 * @param list
	 * @return
	 * Administrator
	 */
	public List<Resourceinfo> listToTree(List<Resourceinfo> list){
		List<Resourceinfo> resultList = new ArrayList<Resourceinfo>();
		if (list != null) {
			for (Resourceinfo resourceinfo : list) {
				if (resourceinfo.getFatherId() == null || resourceinfo.getFatherId() == 0 || resourceinfo.getFatherId() == -1) {//最上级节点
					resultList.add(resourceinfo);
				}
				for (Resourceinfo resource : list) {
					
					if (resource.getFatherId().equals(resourceinfo.getResourceid())) {
						if (resourceinfo.getChildList() == null ) {
							List<Resourceinfo> myChildrens = new ArrayList<Resourceinfo>();
							myChildrens.add(resource);
							resourceinfo.setChildList(myChildrens);
						}else{
							resourceinfo.getChildList().add(resource);
						}
					}
				}
			}
		}
		return resultList;
	}
	
}
